/************************************************************************************
**  
* @copyright (c) 2015-, Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_cap_tcp_service.h
* @version     
* @brief      
* @author   
* @date     2013-01-09
* @note 
*
*  1. 2018-01-09 created this file
* 
*/
#pragma once

#include <string>
#include <map>

#include <duye_type.h>
#include <duye_cap_filter.h>
#include <duye_cap_filter_engine.h>
#include <duye_cap_service_base.h>
#include <duye_cap_packet.h>
#include <duye_cap_tcp_session.h>
#include <duye_cap_netlayer_service.h>

namespace duye {

/**
 * @brief net layer listener interface
 */
class TcpListener : public DataServiceListener {
public:
	virtual ~TcpListener() {}
	virtual int32 onTcpPacket(const PacketReference& pktRef) = 0;
    virtual int32 onTcpSession(const PacketReference& pktRef, const TcpSession& tcpSession) = 0;
};

typedef std::map<TcpListener*, FilterRule*> TcpListenerMap;

class TcpServiceFilterWorker : public FilterWorker {
public:
	TcpServiceFilterWorker() {}
	virtual ~TcpServiceFilterWorker() {}
	virtual bool filterPkt(const PacketReference& pktRef, const FilterRule& rule);
};

/**
 * @brief net layer server interface
 */
class TcpService : public CapDataService, public NetLayerListener, public Singleton<TcpService> {
public:
	TcpService();
	virtual ~TcpService();

	// implementation WdpiService interface 
	virtual bool start();
    // implementation WdpiService interface
	virtual bool stop();
	// implementation WdpiService interface
	virtual bool run();	
	// implementation CapDataService interface
    virtual bool addListener(DataServiceListener* listener, const FilterRule* filterRule = NULL);
	// implementation CapDataService interface
    virtual bool delListener(DataServiceListener* listener);

	// implementation NetLayerListener interface
	virtual int32 onNetLayerPacket(const PacketReference& pktRef);

private:
    TcpListenerMap m_listenerMap;
	TcpServiceFilterWorker m_filterWorker;
	TcpSessionManager m_tcpSessionManager;
};

}
